package com.yuexunit.account.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.yuexunit.core.ConfigPropertites;
import com.yuexunit.util.StringUtil;

/**
 * @author kevis
 */
// TODO[wangjs] 抽离到消息中间件中
public class SMSService {
    private static final String SMS_URL = ConfigPropertites.getProperties("sms.config", "sms.webservice.url", "");
    private static final String SMS_ACCOUNT = ConfigPropertites.getProperties("sms.config", "sms.webservice.account", "");
    private static final String SMS_PASSWORD = ConfigPropertites.getProperties("sms.config", "sms.webservice.password", "");
    private static final String SMS_AUTH = ConfigPropertites.getProperties("sms.config", "sms.webservice.auth", "");
    private static final String SMS_CHANNEL = ConfigPropertites.getProperties("sms.config", "sms.webservice.channel", "");
    private static final String SMS_REGISTER = ConfigPropertites.getProperties("sms.config", "sms.webservice.template.register", "欢迎您注册小悦网诊，这是您的短信验证码:code,有效期为1小时。【小悦门诊】");
    private static final String SMS_FORGET = ConfigPropertites.getProperties("sms.config", "sms.webservice.template.forgetpwd", "");
    private static final String SMS_CHANGE = ConfigPropertites.getProperties("sms.config", "sms.webservice.template.changemobile", "");
    private static final String SMS_TYPE = "1";
    private static String URL_PASSWORD = StringUtil.md5(SMS_PASSWORD + SMS_AUTH).toLowerCase();

    private static SMSService instance = new SMSService();

    public static SMSService getInstance() {
        return instance;
    }

    private SMSService() {

    }

    public boolean sendRegister(String mobile, String verifyCode) {
        return sendSMS(mobile, verifyCode, SMS_REGISTER);
    }

    public boolean sendForgetPwd(String mobile, String verifyCode) {
        return sendSMS(mobile, verifyCode, SMS_FORGET);
    }

    public boolean sendChangeMobile(String mobile, String verifyCode) {
        return sendSMS(mobile, verifyCode, SMS_CHANGE);
    }

    public boolean sendSMS(String mobile, String verifyCode) {
        return sendSMS(mobile, verifyCode, SMS_REGISTER);
    }

    public boolean sendSMS(String mobile, String verifyCode, String template) {
        CloseableHttpClient client = HttpClients.createDefault();
//        SMSResponse smsResponse = new SMSResponse();
        String password = URL_PASSWORD;
        if( password.length() < 32 ){
            int length = 32 - password.length();
            for(int i=0; i<length; i++){
                password = "0" + password;
            }
        }
        String content = template.replaceFirst("code", verifyCode);

        HttpPost post = new HttpPost(SMS_URL);
        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(7);
            nameValuePairs.add(new BasicNameValuePair("account", SMS_ACCOUNT));
            nameValuePairs.add(new BasicNameValuePair("password", password));
            nameValuePairs.add(new BasicNameValuePair("mobile", mobile));
            nameValuePairs.add(new BasicNameValuePair("content", content));
            nameValuePairs.add(new BasicNameValuePair("channel", SMS_CHANNEL));
            nameValuePairs.add(new BasicNameValuePair("smsid", getSmsId()));
            nameValuePairs.add(new BasicNameValuePair("sendType", SMS_TYPE));
            post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "GBK"));

            HttpResponse response = client.execute(post);
            if (response.getStatusLine().getStatusCode() == 200) {
                String result = EntityUtils.toString(response.getEntity()).trim();
                System.out.println("短信发送结果：" + result);
                if (result.startsWith("0")) {
                    return true;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    private  String getSmsId(){
        Date date = new Date();
        java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyyMMddHHmmssSSS");
        return formatter.format(date);
    }



}
